{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a1895e98",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/PineconeDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ca56f0-6ef1-426f-bac5-fd7c374d0f51",
   "metadata": {},
   "source": [
    "# Pinecone Reader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2bd3c59",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2f49003-b952-4b9b-b935-2941f9303773",
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = \"<api_key>\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a7278a1f",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "565f0848",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "262f990a-79c8-413a-9f3c-cd9a3c191307",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.readers.pinecone import PineconeReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "252f8163-7297-44b6-a838-709e9662f3d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = PineconeReader(api_key=api_key, environment=\"us-west1-gcp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53b49187-8477-436c-9718-5d2f8cc6fad0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# the id_to_text_map specifies a mapping from the ID specified in Pinecone to your text.\n",
    "id_to_text_map = {\n",
    "    \"id1\": \"text blob 1\",\n",
    "    \"id2\": \"text blob 2\",\n",
    "}\n",
    "\n",
    "# the query_vector is an embedding representation of your query_vector\n",
    "# Example query vector:\n",
    "#   query_vector=[0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]\n",
    "\n",
    "query_vector = [n1, n2, n3, ...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a88be1c4-603f-48b9-ac64-10a219af4951",
   "metadata": {},
   "outputs": [],
   "source": [
    "# NOTE: Required args are index_name, id_to_text_map, vector.\n",
    "# In addition, we pass-through all kwargs that can be passed into the the `Query` operation in Pinecone.\n",
    "# See the API reference: https://docs.pinecone.io/reference/query\n",
    "# and also the Python client: https://github.com/pinecone-io/pinecone-python-client\n",
    "# for more details.\n",
    "documents = reader.load_data(\n",
    "    index_name=\"quickstart\",\n",
    "    id_to_text_map=id_to_text_map,\n",
    "    top_k=3,\n",
    "    vector=query_vector,\n",
    "    separate_documents=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4baf59e-fc97-4a1e-947f-354a6438ffa6",
   "metadata": {},
   "source": [
    "### Create index "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "109d083e-f3b4-420b-886b-087c8cf3f98b",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SummaryIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e15b9177-9e94-4e4e-9a2e-cd3a288a7faf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"<query_text>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67b50613-a589-4acf-ba16-10571b415268",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
